from new_rmasm import lexer, parser, translator, temp_code, utils


class TranslateResult:

    def __init__(self, result: str, war: str, error: str = None) -> None:
        self.error = error
        self.war = war
        self.result = result


def SimpleTranslate(file_path: str) -> TranslateResult:
    try:
        raw = utils.open_code_file(file_path)
        lex = lexer.Lexer(raw, source=file_path)
        par = parser.AstParser(lex)
        segment = par.segment_ast()
        env = temp_code.AstsTranslateEnv()
        temp_codes = segment.translate(env)
        tct = translator.TCTranslator(temp_codes)
        res = tct.translate()
        if not res.endswith('\nend'):
            res += '\nend'
        return TranslateResult(res, '\n'.join([str(i) for i in env.warnings]))
    except Exception as e:
        return TranslateResult('', '', e)


if __name__ == '__main__':
    r = SimpleTranslate("e:/t.txt")
    if r.error:
        raise r.error
    else:
        if r.war:
            print('\n'.join([str(i) for i in r.war]))
            # print('\n'.join(r.war))
            print('===========================')
        print(r.result)
